<template>
  <div class="custom_page formSearch">
    <div class="content">
      <!-- 顶部搜索 -->
      <div class="toolbar">
        <!-- 顶部搜索 -->
        <el-form :inline="true" :model="searchForm">
          <el-form-item label="客户昵称">
            <el-input
              v-model="searchForm.name"
              maxlength="20"
              placeholder="请输入客户昵称"
            />
          </el-form-item>
          <el-form-item label="客户手机号">
            <el-input
              v-model="searchForm.phone"
              maxlength="11"
              placeholder="请输入客户手机号"
            />
          </el-form-item>
          <el-form-item label="分销员昵称">
            <el-input
              v-model="searchForm.distributorName"
              maxlength="20"
              placeholder="请输入分销员昵称"
            />
          </el-form-item>
          <el-form-item label="绑定状态">
            <el-select v-model="searchForm.ifBind" placeholder="请选择绑定状态">
              <el-option label="全部" value="null" />
              <el-option label="已绑定" value="1" />
              <el-option label="未绑定" value="0" />
            </el-select>
          </el-form-item>
          <el-form-item label-width="0">
            <el-button type="primary" @click="search">查询</el-button>
            <el-button @click="clear">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!--  表格 -->
      <div class="content_table">
        <div class="table">
          <el-table
            v-loading="tableLoading"
            :data="tableData"
            border
            :header-cell-style="{ background: '#EEF3FF', color: '#333333' }"
            style="width: 100%"
            class="dataTable"
          >
            <el-table-column prop="name" label="客户昵称" />
            <el-table-column prop="phone" label="客户手机号" />
            <el-table-column prop="distributorName" label="所属分销员" />
            <el-table-column prop="distributorPhone" label="分销员手机号" />
            <el-table-column label="绑定状态">
              <template #default="scope">
                <span v-if="scope.row.ifBind == 0">未绑定</span>
                <span v-if="scope.row.ifBind == 1">已绑定</span>
              </template>
            </el-table-column>
            <el-table-column prop="updateTime" label="关系更新时间" />
            <el-table-column label="操作" show-overflow-tooltip>
              <template #default="scope">
                <div class="btnList">
                  <el-button link type="primary" @click="relieve(scope.row)">解绑</el-button>
                  <el-button link type="primary" @click="add(scope.row)">绑定</el-button>
                  <el-button link type="primary" @click="del(scope.row)">移除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div class="fenye">
            <el-pagination
              v-model:current-page="searchForm.page"
              :page-sizes="[10, 20, 50, 100]"
              :page-size="searchForm.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              background
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </div>
      </div>
    </div>
    <!-- *****************************弹框开始***************************** -->
    <!-- 解绑/移除弹框 -->
    <el-dialog
      v-model="isVisible.show"
      :title="isVisible.title"
      center
      :close-on-click-modal="false"
      width="30%"
    >
      <div style="text-align: center">
        <div class="unbindimg">
          <img src="../../../assets/images/tixing.png" alt srcset />
        </div>
        <div class="dialog_content">{{ isVisible.text }}</div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="isVisible.show = false">取消</el-button>
          <el-button type="primary" :loading="unbindLoading" @click="agreeEn">确认</el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog
      v-model="isVisible.shows"
      :title="isVisible.title"
      center
      :close-on-click-modal="false"
      width="30%"
    >
      <div style="text-align: center">
        <div style="display: flex" class="diaoCen">
          <el-input
            v-model="searchs"
            maxlength="20"
            style="width: 200px"
            clearable
            placeholder="请输入分销员名称"
          />
          <el-button type="primary" @click="searchDis">查询</el-button>
        </div>
        <el-radio-group v-model="distributorId">
          <el-radio v-for="(item, index) in inviteList" :key="index" :label="item.distributorId">{{ item.distributorName }}</el-radio>s
        </el-radio-group>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="isVisible.shows = false">取消</el-button>
          <el-button type="primary" :loading="bindLoading" @click="agreeEn">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onBeforeMount } from 'vue'
import {
  shipGetAll,
  shipAdd,
  getAllInvitees,
  relieveBind,
  shipDelete,
} from '@/api/distributor'
const searchForm = ref({
  name: '', // 客户昵称
  phone: '', // 客户手机号
  distributorName: '', // 分销员昵称
  ifBind: null, // 绑定状态 是否绑定 1-是 0-否
  page: 1,
  pageSize: 10,
})
const distributorId = ref('')
const isVisible = ref({})
const total = ref(1)
const tableLoading = ref(false)
const tableData = ref([])
const searchs = ref('')
const inviteList = ref([])
const bindLoading = ref(false)
const unbindLoading = ref(false)

onBeforeMount(() => {
  getAll()
  getList()
})
const handleSizeChange = (val) => {
  searchForm.value.pageSize = val
  getAll()
}
const handleCurrentChange = (val) => {
  searchForm.value.page = val
  getAll()
}
const searchDis = () => {
  getList()
}
// 查询
const search = () => {
  total.value = 1
  searchForm.value.page = 1
  getAll()
}
// 绑定
const add = (row) => {
  isVisible.value = {
    shows: true,
    title: '绑定',
    index: 1,
    arr: row,
  }
}
// 解绑
const relieve = (row) => {
  isVisible.value = {
    show: true,
    title: '解绑',
    text: '解绑后对应的分销员将失去绑定关系，并且无法再获得收益',
    index: 2,
    arr: row,
  }
}
// 移除
const del = (row) => {
  isVisible.value = {
    show: true,
    title: '移除',
    text: '移除后客户将从分销员工具消失， 但仍可被其他分销员绑定关系',
    index: 3,
    arr: row,
  }
}
const agreeEn = async () => {
  if (isVisible.value.index === 1) {
    try {
      bindLoading.value = true
      if (distributorId.value === '') {
        return ElMessage.error('请选择分销员')
      }
      const res = await shipAdd({
        buyerUserId: isVisible.value.arr.buyerUserId,
        distributorId: distributorId.value
      })
      if (res.code === '') {
        ElMessage.success('绑定成功')
        getAll()
        searchs.value = ''
        isVisible.value.shows = false
      }
    } finally {
      bindLoading.value = false
    }
  } else if (isVisible.value.index === 2) {
    try {
      unbindLoading.value = true
      const res = await relieveBind({
        buyerUserId: isVisible.value.arr.buyerUserId,
        distributorId: isVisible.value.arr.distributorId,
      })
      if (res.code === '') {
        ElMessage.success('解绑成功')
        getAll()
        isVisible.value.show = false
      }
    } finally {
      unbindLoading.value = false
    }
  } else if (isVisible.value.index === 3) {
    try {
      unbindLoading.value = true
      const res = await shipDelete({
        buyerUserId: isVisible.value.arr.buyerUserId,
        distributorId: isVisible.value.arr.distributorId,
      })
      if (res.code === '') {
        ElMessage.success('移除成功')
        getAll()
        isVisible.value.show = false
      }
    } finally {
      unbindLoading.value = false
    }
  }
}
// 清除
const clear = () => {
  searchForm.value = {
    name: '', // 客户昵称
    phone: '', // 客户手机号
    distributorName: '', // 分销员昵称
    ifBind: null, // 绑定状态 是否绑定 1-是 0-否
    page: 1,
    pageSize: 10,
  }
  getAll()
}
// 初始化查询所有数据
const getAll = async () => {
  try {
    tableLoading.value = true
    const res = await shipGetAll(searchForm.value)
    tableData.value = res.data.list
    total.value = res.data.total
  } finally {
    tableLoading.value = false
  }
}
// 初始化查询所有邀请人
const getList = async () => {
  const res = await getAllInvitees({
    search: searchs.value,
  })
  inviteList.value = res.data
}
</script>

<style lang="scss" scoped>
// .custom_page {
//   padding: 20px 20px 0;
// }
.checkBoxStyle {
  margin-bottom: 20px;
}
.diaoCen {
  display: flex;
  justify-content: center;
  input {
    width: 200px;
  }
  button {
    margin-left: 20px;
  }
}
.el-radio-group {
  margin-top: 20px;
}
:deep(.el-radio__inner) {
  width: 30px;
  height: 30px;
}
:deep(.el-radio__label) {
  font-size: 18px;
}
.dialog_content {
  width: 275px;
  height: 46px;
  margin: auto;
  font-size: 16px;
  font-family: PingFang SC;
  font-weight: 400;
  color: #333333;

  line-height: 30px;
  margin-top: 25px;
}
</style>
